package net.blogjava.mobile.calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;

// 铃声震动提醒
public class AlarmAlert extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();	// 打开本Activity返回Intent对象
		Bundle bundle = intent.getExtras();
		String remindMsg = bundle.getString("remindMsg");		// 传入Key返回Value
		if (bundle.getBoolean("ring")) {	// 判断提醒是否是铃声
			Main.mediaPlayer = MediaPlayer.create(this, R.raw.ring);
			try {
				Main.mediaPlayer.setLooping(true);		// 设置播放器是否要循环
				Main.mediaPlayer.prepare();		// 播放器播放前准备
			}
			catch (Exception e) {
				setTitle(e.getMessage());
			}
			Main.mediaPlayer.start();	// 开始或继续播放
		}
		if(bundle.getBoolean("shake")) {	// 判断提醒是否是震动
			Main.vibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
			Main.vibrator.vibrate(new long[]{1000, 100, 100,1000}, -1);
		}
		new AlertDialog.Builder(AlarmAlert.this).setIcon(R.drawable.clock)
			.setTitle("提醒").setMessage(remindMsg).setPositiveButton("关掉他",
				new DialogInterface.OnClickListener() {		// 当对话框被点击时会触发接口事件来响应一些操作 
					public void onClick(DialogInterface dialog, int whichButton) {
						AlarmAlert.this.finish();	// finish() - Activity结束或者需要关闭
						if (Main.mediaPlayer != null)
							Main.mediaPlayer.stop();	// 播放器停止播放
						if(Main.vibrator != null)
							Main.vibrator.cancel();
					}
				}).show();

	}
}
